<?php
namespace Shama;
use Fox\Dao\Link;

use Fox\Object\Links;
/**
 * Contact Object.
 *
 * It represant a sales Contact Entity.
 *
 * @author Xavier VILAIN
 * @package customer
 * @property int 	$contactid 	Customer Id
 * @property string $keyname		We store customer keyname (to check unicity faster)
 * @property string $title			Title
 * @property string $name1			Name of the customer
 * @property string $name2			Name 2
 * @property int 	$adrid			Address Id (can be null)
 * @property bool 	$active			Is the customer active
 * @property bool 	$loekz 			Deletion Flag if true is an been deleted
 */
class Contact extends \Fox\Object\Versioned {

	/**
	 *
	 * Link to Contact Partners
	 * @var array
	 */
	public static $JOIN_PARTNERS_LEFT = array(
		"source" => "contacts",
		"table" => "contact_partners",
		"short" => "p",
		"foreign" => "partnerid",
		"column" => "contactid",
		"type" => "LEFT JOIN");

	/**
	 * Link to the address
	 */
	const LINK_ADDRESS = "address";

	/**
	 * Link to the partners
	 */
	const LINK_PARTNERS = "partners";

	/**
	 * Link to the partners
	 */
	const LINK_DOCUMENTS = "documents";

	/**
	 * Link to Tasks
	 */
	const LINK_TASKS = "tasks";

	/**
	 * Link to Streams
	 */
	const LINK_STREAMS = "streams";

	/**
	 * Link to Messages
	 */
	const LINK_MESSAGES = "messages";

	/**
	 * Link to the Company
	 */
	const LINK_COMPANY = "company";

	/**
	 * Link to Source
	 */
	const LINK_SOURCE = "source";

	/**
	 * Link to Type
	 */
	const LINK_TYPE = "type";

	/**
	 * Link to Status
	 */
	const LINK_STATUS = "status";

	/**
	 * Type : Leads
	 */
	const TYPE_LEAD = "lead";

	/**
	 * Type : Customer
	 */
	const TYPE_BASIC = "cust";

	/**
	 * Status
	 */
	const STATUS_NEW = "NW";

	/**
	 * REST Service for contact Creation
	 */
	const SERVICE_CREATE = "contact-service";

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::afterSetFields($fields)
	 */
	protected function afterSetFields(array $fields)
	{
		if(isset($fields['name1']))
		{
			$this->keyname = \Fox\Utilities::cleanup($fields['name1']);
		}
		$this->address()->setFields($fields);
		return $fields;
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionCreate()
	 */
	protected function afterVersionCreate(\Fox\Version $version)
	{
		// Add Resume to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->add($this,$this->getId(),self::className());
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionUpdate()
	 */
	protected function afterVersionUpdate()
	{
		// Search Engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->getId(),self::classname());
	}

	/**
	 *
	 * Load all sreams associated to the contact,
	 * those stream are data source that feed us
	 *
	 */
	public function feeder()
	{
		$news = array();
		// Load Streams
		foreach($this->streams() as $stream)
		{
			if(!$stream->exists())
				continue;
			$news = $stream->stream()->dump();
		}
		// Load Messages
		foreach($this->messages() as $message)
		{
			if(!$message->exists())
				continue;
			$news[$message->cdate] = array(
				'type' => $message::className(),
				'object' => $message);
		}
		// Load Events

		// Load Tasks
		foreach($this->tasks() as $task)
		{
			if(!$task->exists())
				continue;
			$news[$task->duedate] = array(
				'type' => $task::className(),
				'object' => $task);
		}
		return $news;
	}
}

\Fox\Dao::infos(Contact::className(),new \Fox\Object\Info('contacts', \Fox\Databases::DB_APP));

Company::className();
// Link to Address Object
Links::add(\Fox\Dao::link(Contact::className(),Contact::LINK_ADDRESS,Link::FETCH_EAGER)
	->to(Address::className(), Address::LINK_CUSTOMER)
	->on('adrid')
	->oneToOne()
	->versionTo()
);

// Link to Contact Documents
\Fox\Object\Links::add(\Fox\Dao::link(Contact\Document::className(),Contact\Document::LINK_CONTACT)
	->to(Contact::className(),Contact::LINK_DOCUMENTS)
	->on('contactid')
	->versionFrom());

// Link to Contact Partners
\Fox\Object\Links::add(\Fox\Dao::link(Contact\Partner::className(),Contact\Partner::LINK_CONTACTS)
	->to(Contact::className(),Contact::LINK_PARTNERS)
	->on('contactid')
	->versionFrom());

// Link to Contact Tasks
\Fox\Object\Links::add(\Fox\Dao::link(Contact\Task::className(),Contact\Task::LINK_CONTACT)
	->to(Contact::className(),Contact::LINK_TASKS)
	->on('contactid')
	->versionFrom());

// Link to Contact Stream
\Fox\Object\Links::add(\Fox\Dao::link(Contact\Stream::className(),Contact\Stream::LINK_CONTACT)
	->to(Contact::className(),Contact::LINK_STREAMS)
	->on('contactid')
	->versionFrom());

// Link to Contact Messages
\Fox\Object\Links::add(\Fox\Dao::link(Contact\Message::className(),Contact\Message::LINK_CONTACT)
	->to(Contact::className(),Contact::LINK_MESSAGES)
	->on('contactid')
	->versionFrom());

// Link to Contact Source
\Fox\Object\Links::add(\Fox\Dao::link(Contact::className(),Contact::LINK_SOURCE)
	->to(Contact\Source::className(),Contact\Source::LINK_CONTACT)
	->oneToOne()
	->on('source')
	);

// Link to Contact Type
\Fox\Object\Links::add(\Fox\Dao::link(Contact::className(),Contact::LINK_TYPE)
	->to(Contact\Type::className(),Contact\Type::LINK_CONTACT)
	->oneToOne()
	->on('type'));

// Link to Contact Status
\Fox\Object\Links::add(\Fox\Dao::link(Contact::className(),Contact::LINK_STATUS)
	->to(Contact\Status::className(),Contact\Status::LINK_CONTACT)
	->on('status')
	->oneToOne());